package club.cearnach.web.controller.admin;

import club.cearnach.web.controller.BaseController;
import club.cearnach.entity.Movie;
import club.cearnach.api.exception.AdminException;
import club.cearnach.api.form.PageBean;
import club.cearnach.api.service.*;
import club.cearnach.api.vo.admin.AdminHomeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.concurrent.Callable;

/**
 * @author 阮胜
 * @date 2018/5/15 8:17
 */
@Api(tags = "后台首页控制类")
@Controller
@RequestMapping("/admin")
@Slf4j
public class BackgroundHomeController extends BaseController {
    public static final String ADMIN_INDEX = "admin/index";
    private static final String TOTAL_VISITORS = "totalVisitors";
    private static final String ONLINE_VISITORS = "onlineVisitors";
    private final StringRedisTemplate stringRedisTemplate;
    private final ICommentService commentService;

    public BackgroundHomeController(IActorService actorService, IAdminService adminService, IDirectorService directorService, IMovieLanguageService movieLanguageService, IMovieService movieService, IMovieTypeService movieTypeService, IPersonService personService, IPlaceService placeService, IVisitorService visitorService, StringRedisTemplate stringRedisTemplate, ICommentService commentService) {
        super(actorService, adminService, directorService, movieLanguageService, movieService, movieTypeService, personService, placeService, visitorService);
        this.stringRedisTemplate = stringRedisTemplate;
        this.commentService = commentService;
    }

    @ApiOperation("后台主页,初始化首页数据到 AdminHomeVO 值对象模型中.(多线程处理)")
    @GetMapping("/index")
    public Callable<Model> index(Model model) {
        return () -> {
            String adminAccount = SecurityContextHolder.getContext().getAuthentication().getName();

            ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
            AdminHomeVO adminHomeVO = new AdminHomeVO();
            PageBean defaultPageBean = new PageBean(0, 10);
            Page<Movie> recentMovieListPage = movieService.findAllOrderByInitialReleaseDateDesc(defaultPageBean);
            adminHomeVO.setAdmin(adminService.findByAccount(adminAccount).orElseThrow(() -> new AdminException(AdminException.ADMIN_CAN_NOT_FOUNT)));
            adminHomeVO.setHotMovieList(movieService.findHotMovie(10));
            adminHomeVO.setRecentMovieList(recentMovieListPage.getContent());
            adminHomeVO.setMovieCount(movieService.count());
            adminHomeVO.setVisitorCount(visitorService.count());
            adminHomeVO.setMovieCount(movieService.count());
            adminHomeVO.setTodayVisitorCount(visitorService.countByVisitDate());
            adminHomeVO.setVisitorCount(Long.valueOf(ops.get(TOTAL_VISITORS)));
            adminHomeVO.setOnlineCount(Long.valueOf(ops.get(ONLINE_VISITORS)));
            adminHomeVO.setUnreadCommentList(commentService.obtainUnreadComments(0, 10));
            adminHomeVO.setRecentVisitorList(visitorService.findAllOrderByVisitDate(defaultPageBean).getContent());
            adminHomeVO.setRecentLoginAdminList(adminService.findAllOrderByCreateDate(defaultPageBean).getContent());
            model.addAttribute(adminHomeVO);
            return model;
        };
    }


    @ApiOperation("后台自动转发页面")
    @GetMapping("/*")
    public void everyPage() {

    }



}